#include <stdio.h>
#include "time.h"
#include <conio.h>
#include "sline.h"
#include <stdlib.h>
#if defined(USE_SINTP_OP) || defined(USE_SINTP_OP546)
#include <sintp.h>
#endif
#ifdef USE_TREE_OP
#include "tree.h"
#endif
#ifdef USE_YMODEM_OP
#include "ymodem_port.h"
#endif
#include "ck_config.h"
#include "stdint.h"
#include "win_uart.h"

win_uart_t *puart = LR_NULL;

/**
 * @brief 字符打印
 * 
 * @param argc 
 * @param argv 
 * @return int 
 */
int my_echo(int argc ,char * argv[])
{
  if(argc ==2 )
  {
    printf("%s\r\n",argv[1]);
  }
  else
  {
    printf("input para is error!\r\n");
  }
  return 0;
}


int cmd_len(int argc ,char * argv[])
{
  int i = 0;
  for (;i<argc;i++)
  {
    sl_printf("argc = %d %d:%s\r\n",argc,i,argv[i]);
  }
  return 0;
}

/**
 * @brief 退出软件
 * 
 * @param argc 
 * @param argv 
 * @return int 
 */
int my_exit(int argc ,char * argv[])
{
  printf("shutdown!\r\n");
  // delay(1500);
  exit(0);
}
char buf[1024*1024*1] = {};
#ifdef USE_YMODEM_OP
int ymodem(int argc ,char * argv[])
{
  int file_size = -1;
  sl_printf("start ymodem recv. \r\n");
  file_size = ymodem_recv((long)buf);
  return file_size;
}
#endif
void bufread(int argc,char * argv[])
{
	if(argc ==3)
	{
		uint8_t * p = (uint8_t*)(atoi(argv[1])+buf);
		uint16_t num = (uint16_t)atoi(argv[2]);
		int i = 0 ;
		for (i = 0 ; i< num ; i++)
		{
			sl_printf("[%2x]	",p[i]);
			if( (i+1)%10 == 0x00)
			{
				sl_printf("\r\n");
			}
		}
	}
	else
	{
		sl_printf("Input error\r\n");
	
	}
}

#include "sline.h"
#include "lua/lua.h"
#include "lua/lauxlib.h"
#include "lua/lualib.h"
int open_lua(int argc ,char * argv[])
{
    // 文件指针
    FILE *file;
    // 字符数组用于存储文件内容
    char buffer[1024*512] = {0};
    // 文件大小
    long fileSize;
    char filename[] = "fc.lua";  // 要打开的文件名

    // 打开文件
    file = fopen(filename, "rb");
    if (file == NULL) {
        perror("Error opening file");
        return EXIT_FAILURE;
    }

    fseek(file, 0, SEEK_END);
    fileSize = ftell(file);
    fseek(file, 0, SEEK_SET);
    // 一次性读取文件内容
    fread(buffer, fileSize, 1, file);


    // 关闭文件
    fclose(file);

    // 输出文件内容（示例用途）
    // printf("File content:\n%s\n", buffer);

    lua_State *Lp = luaL_newstate();
    luaL_openlibs(Lp);
    int ret = luaL_dostring(Lp,buffer);
    lua_close(Lp);
}

/**
 * @brief windows下sline演示例子
 * 
 */
void test_win_create(void)
{
  puart = lr_get_win_uart_handle(LR_NULL);
#ifdef USE_YMODEM_OP
  ymodem_port_st ym = {
    .getc = (void *)puart->ops.read,
    .putc = (void *)puart->ops.write,
    .usdelay = (void *)puart->user.usdelay,
    .tschar = (void *)puart->user.tschar
  };
  set_ymodem_port(&ym);
  #endif

  sl_register_func_t sl_rfunc = {
      .getc = (void *)puart->ops.read,
      .putc = (void *)puart->ops.write
  };


  sl_cmd_export_pre("echo",my_echo,"printf text.");
  sl_cmd_export_pre("echo1",my_echo,"printf text.");
  sl_cmd_export_pre("echo2",my_echo,"printf text.");
  sl_cmd_export_pre("cmd",cmd_len,"printf text.");
  sl_cmd_export_pre("exit",my_exit,"kill this ware.");
  #ifdef USE_TREE_OP
  sl_cmd_export_pre("tree",tree_test,"tree_test.");
  sl_cmd_export_pre("bstree",bstree_test,"tree_test.");
  #endif
  #ifdef USE_YMODEM_OP
  sl_cmd_export_pre("ymodem",ymodem,"ymodem.");
  #endif
  sl_cmd_export_pre("bufread",bufread,"bufread.");

#if defined(SL_OUTPUT_ENABLE) && (defined(USE_LUA_OP) || defined(USE_SINTP_OP546))
  sl_cmd_export_pre("lua_enable",lua_enable,"open lua env.");
  sl_cmd_export_pre("open_lua",open_lua,"open lua file.");
  #endif

  #if(SL_KEYWORD_ENABLE)
  char* keys[50]= {"print()","key2","key3","key4","if",
                  "then","else","zhuxuanlin","sline","end",
                  "while","class","and","break","do"
                  ,"elseif","false","for","function","in",
                  "local","nil","not","or","repeat","return",
                  "true","until"};
  sl_keys_add(keys,50);
  #endif
  
  sl_create( &sl_rfunc);
}
